package com.luckku.boot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luckku.boot.mapper.CartMapper;
import com.luckku.boot.model.entity.Cart;
import com.luckku.boot.model.vo.CartInfoVo;
import com.luckku.boot.model.vo.CartVo;
import com.luckku.boot.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luckku
 * @date 2021/12/16
 */

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    CartMapper cartMapper;

    @Override
    public List<CartVo> getCartById(String userId, String storeId) {
        return cartMapper.getCartById(userId, storeId);
    }

    @Override
    public CartInfoVo getUserCart(String userId, String storeId) {
        List<CartVo> list = getCartById(userId, storeId);
        CartInfoVo cartInfoVO = new CartInfoVo();
        cartInfoVO.setCartVoList(list);
        float count=0;
        for (CartVo cartVO : list) {
            count+=cartVO.getPrice()*cartVO.getCount();
        }
        cartInfoVO.setTotalPrice(count);
        return cartInfoVO;
    }

    @Override
    public void deleteCart(String userId, String storeId) {
        List<CartVo> list = getCartById(userId, storeId);
        List<String> cartIdList = new ArrayList<>();
        for (CartVo cartVO : list) {
            cartIdList.add(cartVO.getCartId());
        }
        cartMapper.deleteBatchIds(cartIdList);
    }
}
